<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class DepartmentController extends BaseController {

    public function __construct() {
        parent::__construct();
        $is_admin = session('is_admin');
        if(!$is_admin)
            $this->error('您无权操作', U('Reporter/index'));
    }

    public function index(){
        $search=trim(I('get.search',''));
        $pageNum=15;
        $p=I('get.p',0);
        $where['org_id']=$this->adminInfo['organ'];

        if($search){
            $where['name']=array('like',"%{$search}%");
        }

        $res=M('channel')->where($where)->page($p.','.$pageNum)->order('chid DESC')->select();
        
        $count      = M('channel')->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$pageNum);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('depList',$res);
        $this->assign('active','组织架构');
        C('TOKEN_ON',false);
        $this->display();
    }



    public function addDepartment(){
        if(IS_POST){
            $channel = M('Channel');
            $data['name']=I('post.depName');
            if($data['name']){
                $data['org_id']=$this->adminInfo['organ'];
                $data['status']=1;
                $res=$channel->add($data);
                if($res){
                    $this->success('部门添加成功！',U('index'));
                }else{
                    $this->error('服务器错误！');
                }
            }else{
                $this->error('参数错误！');
            }
        }else{
            $this->assign('active','组织架构');
            $this->display();
        }
    }

    public function editDepartment(){
        $chid=I('get.chid');
        if(IS_POST){
            $channel = M('Channel');
            $data['name']=I('post.depName');
            $data['chid']=I('post.chid');
            if($data['name']){
                $res=$channel->save($data);
                if($res===false){
                    $this->error('服务器错误！');
                }else{
                    $this->success('部门修改成功！',U('index'));
                }
            }else{
                $this->error('参数错误！');
            }
        }else{

            $res=M('Channel')->find($chid);

            $this->assign('department',$res);
            $this->assign('active','组织架构');
            $this->display();
        }
    }

    public function setDepartmentStatus(){
        $chid=I('get.chid');
        if($chid && preg_match("/^\d+$/",$chid)){
            $status=M('Channel')->getFieldByChid($chid,'status');
            $status=!$status;
            $res=M('Channel')->save(array('status'=>$status,'chid'=>$chid));
            $mess['status']=$res;
            $mess['info']=$status;
            $this->ajaxReturn($mess);
        }
    }

    public function queryDepartment(){
        $where['name']=I('post.param');

        $where['org_id']=$this->adminInfo['organ'];
        if($where['name']){
            $res=M('channel')->where($where)->find();
            if($res && $res['chid']!=I('get.chid')){
                $mess['status']='n';
                $mess['info']='此部门已经存在！';
            }else{
                $mess['status']='y';
                $mess['info']='可以使用';
            }
        }

        $this->ajaxReturn($mess);
    }
}